/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package solucionessgh.EstadoCivil;

/**
 *
 * @author roderick
 */

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import solucionessgh.Util.DataBase;

/**
 * @author roderick
 **/

public class EstadoCivilModel extends DataBase{

    private int id;
    private String ecvdesc;
    private String ecvabr;

    public EstadoCivilModel(){

    }

    private boolean validar(){
        /*if(this.ecvdesc.length()>20){
            JOptionPane.showMessageDialog(null,
            "Descripcion menor a 20 digito",
            "Nivel Usuario",
            JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if(this.ecvabr.length()!=3){
            JOptionPane.showMessageDialog(null,
            "Codigo debe tener tres digito",
            "Nivel Usuario",
            JOptionPane.ERROR_MESSAGE);
            return false;
        }*/
        return true;

    }

    public boolean create(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("insert into estadocivil(ecvdesc,ecvabr) values(?,?);");
                stmt.setString(1,this.ecvdesc);
                stmt.setString(2,this.ecvabr);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public boolean update(){
        //if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("update estadocivil set ecvdesc = ? ,ecvabr = ? where ecvcod = ? ;");
                stmt.setString(1,this.ecvdesc);
                stmt.setString(2,this.ecvabr);
                stmt.setInt(3, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        //}
        return false;
    }

    public boolean delete(){
        //if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("delete from estadocivil where ecvcod=?;");
                stmt.setInt(1, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        //}
        return false;
    }

    public ResultSet read(){
        return this.find("SELECT * FROM estadocivil order by ecvdesc ");
    }


    public ResultSet finder(String var){
        return this.find("SELECT ecvcod, ecvdesc, ecvabr FROM estadocivil WHERE ecvdesc LIKE '"+var+"%';");
    }

    public ResultSet finder(int var){
        return this.find("SELECT ecvcod, ecvdesc, ecvabr FROM estadocivil WHERE ecvcod = "+var+";");
    }



    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getecvabr() {
        return ecvabr;
    }

    public void setecvabr(String ecvabr) {
        this.ecvabr = ecvabr;
    }

    public String getecvdesc() {
        return ecvdesc;
    }

    public void setecvdesc(String ecvdesc) {
        this.ecvdesc = ecvdesc;
    }
}
